package com.blqy.fr.user.api;

import com.blqy.fr.common.annotation.BlQyLogin;
import com.blqy.fr.common.execption.BlQyException;
import com.blqy.fr.common.result.Result;
import com.blqy.fr.common.result.ResultCodeEnum;
import com.blqy.fr.common.util.AuthContextHolder;
import com.blqy.fr.dto.SwitchTeamDto;
import com.blqy.fr.user.service.TeamUserService;
import com.blqy.fr.vo.team.TeamListItemVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "主页的团队信息接口")
@RestController
@Slf4j
@RequestMapping("/api/page/team")
public class HomePageTeamController {
    @Autowired
    private TeamUserService teamUserService;

    @GetMapping("/list")
    @Operation(summary = "获取用户可切换的团队列表")
    @BlQyLogin
    public Result<List<TeamListItemVo>> getTeamList() {
        Long userId = AuthContextHolder.getUserId();
        List<TeamListItemVo> list = teamUserService.getTeamList(userId);
        return Result.ok(list);
    }

    @PostMapping("/switch")
    @Operation(summary = "切换当前团队")
    public Result<?> switchCurrentTeam(@Valid @RequestBody SwitchTeamDto dto) {
        teamUserService.switchCurrentTeam(dto.getTeamId());
        return Result.ok();
    }

    @GetMapping("/sbJun/{userId}")
    @BlQyLogin
    public Result<Long> getTeamIdByUserId(@PathVariable Long userId) {
        if (userId == null) {
            throw new BlQyException(ResultCodeEnum.PARAM_ERROR);
        }
        Long teamId = teamUserService.getTeamIdByUserId(userId);
        return Result.ok(teamId);
    }
}
